package com.heima.schedule.feign;

import com.heima.feign.schedule.IScheduleClient;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.schedule.dtos.TaskDto;
import com.heima.schedule.service.TaskService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

@Slf4j
@RestController
public class ScheduleClient implements IScheduleClient {
    @Autowired
    private TaskService taskService;

    /**
     * 添加任务
     *
     * @param dto
     * @return 返回任务id
     */
    @PostMapping("/api/v1/task/add")
    public ResponseResult addTask(TaskDto dto) {
        return ResponseResult.okResult(taskService.addTask(dto));
    }

    /**
     * 按照类型和优先级拉取任务
     *
     * @param taskType 任务类型
     * @param priority 任务优先级
     * @return 返回任务
     */
    @GetMapping("/api/v1/task/poll/{taskType}/{priority}")
    public ResponseResult popTask(@PathVariable("taskType") Integer taskType, @PathVariable("priority") Integer priority) {
        return ResponseResult.okResult(taskService.popTask(taskType, priority));
    }
}
